Restructured, minor improvements

Have restructured code, renamed variables and functions, made some
minor code improvements