Cosmetic code changes. Reformatted structure and splitted functions so code will be more clear.