Cleaned up and documented