Reorganized code a bit