Added input events, improved state handling