diff --git a/main.go b/main.go
index 01f6972403147dcfdac418afda7eee9b69a715fc..b0db335ab52f3495b81e80b09cc889aee138f843 100644
--- a/main.go
+++ b/main.go
@@ -278,53 +278,29 @@ func main() {
 			if err != nil {
 				log.Error(err)
 			}
-			pageContent := string(contents)
-			//Check for client_id
-			clientIdStartIndex := strings.Index(pageContent, "client_id=")
-			if clientIdStartIndex == -1 {
-				log.Error("No client_id found")
-				http.Error(w, http.StatusText(500), 500)
+
+			params, err := url.ParseQuery(string(contents))
+			if err != nil {
+				log.Error(err)
+				http.Error(w, err.Error(), 500)
+				return
 			}
-			clientIdStartIndex += 10
-			clientId = pageContent[clientIdStartIndex : clientIdStartIndex+36]
-			//Check for client_secret
-			clientSecretStartIndex := strings.Index(pageContent, "client_secret=")
-			if clientSecretStartIndex == -1 {
-				log.Error("No client_secret found")
-				http.Error(w, http.StatusText(500), 500)
+			if val, ok := params["client_id"]; ok {
+				clientId = val[0]
 			}
-			clientSecretStartIndex += 14
-			clientSecret = pageContent[clientSecretStartIndex : clientSecretStartIndex+36]
-			//Check for code
-			codeStartIndex := strings.Index(pageContent, "code=")
-			if codeStartIndex == -1 {
-				log.Info("No code found (optional param)")
-			} else {
-				codeStartIndex += 5
-				code = pageContent[codeStartIndex : codeStartIndex+30]
+			if val, ok := params["client_secret"]; ok {
+				clientSecret = val[0]
 			}
-			//Check for grant_type
-			grandTypeStartIndex := strings.Index(pageContent, "grant_type=")
-			if grandTypeStartIndex == -1 {
-				log.Error("No grant_type found")
-				http.Error(w, http.StatusText(500), 500)
+			if val, ok := params["code"]; ok {
+				code = val[0]
 			}
-			grandTypeStartIndex += 11
-			tempGrandTypeString := pageContent[grandTypeStartIndex:]
-			grandTypeEndIndex := strings.Index(tempGrandTypeString, "&")
-			if grandTypeEndIndex == -1 {
-				log.Error("No closing tag for grant_type found")
-				http.Error(w, http.StatusText(500), 500)
+			if val, ok := params["grant_type"]; ok {
+				grantType = val[0]
 			}
-			grantType = tempGrandTypeString[0:grandTypeEndIndex]
-			//Check for refresh_token
-			refershTokenStartIndex := strings.Index(pageContent, "refresh_token=")
-			if refershTokenStartIndex == -1 {
-				log.Error("No refresh_token found")
-				http.Error(w, http.StatusText(500), 500)
+			if val, ok := params["refresh_token"]; ok {
+				refreshToken = val[0]
 			}
-			refershTokenStartIndex += 14
-			refreshToken = pageContent[refershTokenStartIndex : refershTokenStartIndex+46]
+			
 		} else {
 			// Retrieve params from query
 			clientId = query.Get("client_id")
@@ -409,53 +385,25 @@ func main() {
 			contents, err := ioutil.ReadAll(r.Body)
 			if err != nil {
 				log.Error(err)
+				http.Error(w, err.Error(), 500)
+				return
 			}
-			pageContent := string(contents)
-			//Check for client_id
-			clientIdKey := "client_id="
-			clientIdLength := 22
-			clientIdStartIndex := strings.Index(pageContent, clientIdKey)
-			if clientIdStartIndex == -1 {
-				log.Error("No client_id found")
-				http.Error(w, http.StatusText(500), 500)
-			}
-			clientIdStartIndex += len(clientIdKey)
-			clientId = pageContent[clientIdStartIndex : clientIdStartIndex+clientIdLength]
-			//Check for client_secret
-			clientSecretKey := "client_secret="
-			clientSecretLength := 16
-			clientSecretStartIndex := strings.Index(pageContent, clientSecretKey)
-			if clientSecretStartIndex == -1 {
-				log.Error("No client_secret found")
-				http.Error(w, http.StatusText(500), 500)
+
+			params, err := url.ParseQuery(string(contents))
+			if err != nil {
+				log.Error(err)
+				http.Error(w, err.Error(), 500)
+				return
 			}
-			clientSecretStartIndex += len(clientSecretKey)
-			clientSecret = pageContent[clientSecretStartIndex : clientSecretStartIndex+clientSecretLength]
-			//Check for code
-			codeKey := "code="
-			codeLength := 27
-			codeStartIndex := strings.Index(pageContent, codeKey)
-			if codeStartIndex == -1 {
-				log.Info("No code found (optional param)")
-			} else {
-				codeStartIndex += len(codeKey)
-				code = pageContent[codeStartIndex : codeStartIndex+codeLength]
+			if val, ok := params["client_id"]; ok {
+				clientId = val[0]
 			}
-			//Check for grant_type
-			grandTypeKey := "grant_type="
-			grandTypeStartIndex := strings.Index(pageContent, grandTypeKey)
-			if grandTypeStartIndex == -1 {
-				log.Error("No grant_type found")
-				http.Error(w, http.StatusText(500), 500)
+			if val, ok := params["client_secret"]; ok {
+				clientSecret = val[0]
 			}
-			grandTypeStartIndex += len(grandTypeKey)
-			tempGrandTypeString := pageContent[grandTypeStartIndex:]
-			grandTypeEndIndex := strings.Index(tempGrandTypeString, "&")
-			if grandTypeEndIndex == -1 {
-				log.Error("No closing tag for grant_type found")
-				http.Error(w, http.StatusText(500), 500)
+			if val, ok := params["grant_type"]; ok {
+				grantType = val[0]
 			}
-			grantType = tempGrandTypeString[0:grandTypeEndIndex]
 
 		} else {
 			// Retrieve params from query