Made some Reddit suggested updates, abstracted some functions and added comments