improved documentation, and re-arranged code