cleaned up the code a little bit and commented a lot