refactored code to get rid of the most warnings