merged stage 2 into 1 and 3. needs proper testing of stage 1 and 3