Fixed warnings and rearranged code