Cleaned up code and comments for public sharing