code cleaned up and commented