Fixed Bug for positioning nested elements, some code cleanup