same problem as before but with far less code and more readable