Bugs fixes

Bugs fixes and new functions