Added stateManager
Added attach functionality. Refatoring.