Added comments and renamed some functions to arguably better names