Refactored code. Fixed some issues.