Bug fixes and code cleaned