Refactor codebase

Move stuff around, and fix some errors as well, and plug possible errors with escaping function.