Got things working, cleaned things up and added comments