Added comments and rearranged code a little