Reorganized code a little