Bugfix and tests improvements