added comments and cleaned up, updated API