Reordered code to make it work