Tests added, fix bugs