Cleaned up code and added some coments