improved implementation
fixed bugs