cleaned up some code, made it easier to read

functions now work perfectly. more to be done still.

SA etc