Cleaned code. Moved repetitive code to one place