used some fixes from Stackoverflow