removed lots of repeated code