Refactoring 

@kishiamy please take a look

thanks!