Refactored, improved documentation