refactored code and structered it a bit