@Override
	public void applyTransaction(long txID) throws UnfulfillableTransactionException, InvalidTransactionStateException {
		Transaction tx = null;
		synchronized(transactions) {
			if (transactions.containsKey(txID)) {
				throw new InvalidTransactionStateException(txID);
			}
			tx = transactions.get(txID);
		}
		synchronized(tx) {
			try {
				tx.setStatus(Status.FINISHING);
				if (!isValidTransaction(txID)) {
					throw new UnfulfillableTransactionException(txID);
				}
				//TODO UUUUgly. Should probably break down to the account level and merge tx where possible.
				for (long k: tx.getDepositsByAccount().keySet()) {
					for (Asset a: tx.getDepositsByAccount().get(k)) {
						accounts.get(k).deposit(a);
					}
				}
				for (long k: tx.getWithdrawsByAccount().keySet()) {
					for (Asset a: tx.getWithdrawsByAccount().get(k)) {
						accounts.get(k).withdraw(a);
					}
				}
				tx.setStatus(Status.FINISHED);
			} catch (InvalidTransactionStateTransitionException ex) {
				throw new InvalidTransactionStateException("Couldn't apply the transaction", txID, tx.getStatus(), ex);
			}
		}
	}