refaactored a bit