Rearranged some code. This might fix some issues.