Clean up code, add doc