Bugs fixing and tests