Refactored code & fixed some little problems