Made more rearrangements, fixed some null resource unkown exception