added some more logic/seperated existing code out