Fixed errors and treated some corner cases.
Added implementations of Object's methods equals, hashCode, toString.