Unit tests added

Added a lot of unit tests and fixed some problems