Fixed a number of errors, added tests