simplified code.
functionality improvments; added comments.