Reformatting, most functions and variables renamed for consistency, some function rewrites for reusability