Fixed variables and cleaned up code