More Bug Fixes

A lot of bug fixes and also added a constants and common file. Also now
the whole thing can be imported as a module.