Refactored and fixed for Windows Phone