added a few variables and cleaned up code