Tests and bugs fixed