cleaned things up, restructured code, fixed wrong index in data vector, added error checking and basic comments