Tests added. Some bugfixes