Cracked the entry/exit rulse logic finally, refactored the code - simplier and readable