added local declarations for better readability
Functions renamed
added comment