structure this code better