Fixed code and bugs