refactored and bugfixed and specs fixed