changed operation design and decoupled code for redability and understanding