Added partial unit testing and fixed some bugs