Refactoring most of the code for easy reading

Signed-off-by: Oscar Suro <outercript@gmail.com>