Fixed selecting shape bug
Refactored a bit the code