Restructured to be closer to the android version.