Moved functions to separate file, fixed some errors, updated and added documentation.