Reorganized the code a bit and added jsdoc

Some clean up and some test to make sure that everything works
Added events to the realm