improved readability by reordering functions and adding comments, added TODOs