Added logic for level end and level transition