Tests added, some bugs fixed