renamed some variables and reorganized code

Change-Id: Ib82d7d32b8db59c88095e492f79f3b8f31c10ccb