Refactoring wallet code