fixed some edege cases + code structure