Applying solutions from stackoverflow.