added comments and improved representation-independence of code