refactored code into some functions

Signed-off-by: Martin Ankerl <martin.ankerl@gmail.com>