Cleaned up the code, added documentation and added proper handling of UTF-8